/* NOTICE
 
    Jim - the Java Image Manager, copyright (c) 2005 - 2007 Grant Gardner 
 
    Jim is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Jim is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
  
    In addition, as a special exception, you are permitted to link this code
    with the 3rd party libraries required to compile this file and to
    distribute a linked combination including them. You must obey the
    GNU General Public License in all respects for all of the code except for
    those 3rd party libraries that are licenced under terms incompatible with the
    GNU General Public License.  
 
 
 NOTICE */

/*
 * Created on 25/04/2004
 */

package au.com.lastweekend.jim.io.tiff;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import au.com.lastweekend.jim.io.exif.ExifTransform;
import au.com.lastweekend.jim.io.exif.ThumbnailTiffDirectory;
import au.com.lastweekend.jim.io.exif.ThumbnailTiffDirectoryWriter;
import au.com.lastweekend.jim.io.exif.TiffTagFormat;

/**
 * Write Tiff files from metadata.
 * 
 * Actually only knows how to turn ThumbnailTiffDirectory into a real Tiff
 * 
 * @version $Id: JpegWriter.java,v 1.2 2005/06/09 11:15:36 ggardner Exp $
 * @author <a href="mailto:grant@lastweekend.com.au"> <Grant Gardner> </a>
 */
public class TiffWriter {

    public TiffWriter() {

    }

    /**
     * Write metadata containing Image to a file according to the transformations of this writer. Note that no transformation on
     * the metadata itself will be done.
     * 
     * @param thumbnailTiffDirectory
     * @param file
     * @throws IOException
     * 
     */
    public void writeImage(ThumbnailTiffDirectory thumbnailTiffDirectory, File file) throws IOException {

        OutputStream outStream = getBufferedOutputStream(file);
        writeImage(thumbnailTiffDirectory, outStream);
        closeStream(outStream);

    }

    /**
     * Write image to the supplied output stream.
     * 
     * @param thumbnailTiffDirectory
     * @param output
     * 
     * @throws IOException
     * 
     */
    public void writeImage(ThumbnailTiffDirectory thumbnailTiffDirectory, OutputStream output) throws IOException {

        ThumbnailTiffDirectoryWriter thumbnailTiffDirectoryWriter = new ThumbnailTiffDirectoryWriter(thumbnailTiffDirectory,
                ExifTransform.NO_TRANSFORM);
        thumbnailTiffDirectoryWriter.setDataFormat(TiffTagFormat.getInstance(thumbnailTiffDirectory.isMotorolaByteOrder()));
        thumbnailTiffDirectoryWriter.setStartOffset(8);
        thumbnailTiffDirectoryWriter.writeHeader(output);
        thumbnailTiffDirectoryWriter.writeDirectory(output);
    }

    private BufferedOutputStream getBufferedOutputStream(File jpegFile) throws FileNotFoundException {

        OutputStream outStream = new FileOutputStream(jpegFile);
        return new BufferedOutputStream(outStream);

    }

    private void closeStream(OutputStream outStream) throws IOException {

        outStream.close();

    }
}